package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.bo.SysRoleGroupMenuBo;
import com.zp.system.entity.vo.SysRoleGroupMenuVo;
import com.zp.system.service.ISysRoleGroupMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Map;

/**
 * 自定义服务与菜单对照控制器
 * 前端访问路由地址为:/system/groupMenu
 *
 * @author zhang peng
 * @date 2022-12-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/groupMenu")
public class SysRoleGroupMenuController extends BaseController {

    private final ISysRoleGroupMenuService iSysRoleGroupMenuService;

    /**
     * 查询自定义服务与菜单对照列表
     */
    @SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public TableDataInfo<SysRoleGroupMenuVo> list (SysRoleGroupMenuBo bo, PageQuery pageQuery) {
        return iSysRoleGroupMenuService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取自定义服务与菜单对照详细信息
     *
     * @param roleGroupId 主键
     */
    @SaCheckPermission("system:menu:query")
    @GetMapping("/{roleGroupId}")
    public R<SysRoleGroupMenuVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String roleGroupId) {
        return R.ok(iSysRoleGroupMenuService.queryById(roleGroupId));
    }

    /**
     * 新增自定义服务与菜单对照
     */
    @SaCheckPermission("system:menu:add")
    @Log(title = "自定义服务与菜单对照", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@RequestBody SysRoleGroupMenuBo bo) {
        return toAjax(iSysRoleGroupMenuService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改自定义服务与菜单对照
     */
    @SaCheckPermission("system:menu:edit")
    @Log(title = "自定义服务与菜单对照", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysRoleGroupMenuBo bo) {
        return toAjax(iSysRoleGroupMenuService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除自定义服务与菜单对照
     *
     * @param roleGroupIds 主键串
     */
    @SaCheckPermission("system:menu:remove")
    @Log(title = "自定义服务与菜单对照", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleGroupIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] roleGroupIds) {
        return toAjax(iSysRoleGroupMenuService.deleteWithValidByIds(Arrays.asList(roleGroupIds), true) ? 1 : 0);
    }

    /**
     * 自定义服务列表树
     *
     * @param roleGroupId 自定义服务主键
     */
    @SaCheckPermission("system:dept:list")
    @PostMapping("/roleGroupMenuTree/{roleGroupId}")
    public R<Map<String, Object>> roleGroupMenuTree (@NotEmpty(message = "主键不能为空") @PathVariable String roleGroupId) {
        return R.ok(iSysRoleGroupMenuService.roleGroupMenuTree(roleGroupId));
    }
}
